﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNLTHD_GoogleMapAPI_DAO;
using CNLTHD_GoogleMapAPI_DTO;
using CNLTHD_GoogleMapAPI.UserControl;

namespace CNLTHD_GoogleMapAPI
{
	public partial class MyMap : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            AjaxPro.Utility.RegisterTypeForAjax(typeof(MyMap));
			if (!IsPostBack)
			{

				if (Request.QueryString["TenDiaDiem"] != null && Request.QueryString["MaDiaDiem"] != null)
				{
					string tenDiaDiem = Request.QueryString["TenDiaDiem"];
					string maDiaDiem = Request.QueryString["MaDiaDiem"];
					string text = Request.QueryString["text"];
				}

				if (Request.QueryString["action"] == "xoa")
					DiaDiemDAO.XoaDiaDiem(Int32.Parse(Request.QueryString["maDiaDiem"]));
				this.onLoad();
			}
		}

        [AjaxPro.AjaxMethod]
        public bool xoaMarket(int _maDiaDiem)
        {
            bool kq = DiaDiemDAO.XoaDiaDiemAnToan(_maDiaDiem);
            return kq;
        }


        [AjaxPro.AjaxMethod]
        public bool reloadTreeView()
        {
            //TreeViewMarker.
            //onLoadTreeView();
            //UpdatePanel1.Update();
            return true;
        }

        [AjaxPro.AjaxMethod]
        public bool themChiaSeDiaDiem(string tenNguoiNhan, int maDiaDiem)
        {
            string sTenDangNhap = (string)Session["TenDangNhap"];
            NguoiDung _nguoiChiaSe = NguoiDungDAO.LayNguoiDung(sTenDangNhap);
            NguoiDung _nguoiNhan = NguoiDungDAO.LayNguoiDung(tenNguoiNhan);

            return ChiaSeDiaDiemDAO.ThemChiaSeDiaDiem(_nguoiChiaSe.MaNguoiDung, _nguoiNhan.MaNguoiDung, maDiaDiem);
        }

        [AjaxPro.AjaxMethod]
        public bool kiemTraTaiKhoan(string tenTaiKhoan)
        {
            return NguoiDungDAO.kiemTraTaiKhoan(tenTaiKhoan);
        }
        [AjaxPro.AjaxMethod]
        public bool xoaChiaSeAnToan(int maDiaDiem)
        {
            return ChiaSeDiaDiemDAO.XoaChiaSeAnToan(maDiaDiem);
        }

        [AjaxPro.AjaxMethod]  // Cái này thêm vào địa điểm
        public bool chapNhanChiaSeDiaDiem(int maDiaDiem)
        {
            return ChiaSeDiaDiemDAO.XoaChiaSeAnToan(maDiaDiem);
        }

        [AjaxPro.AjaxMethod] 
        public int layMaLoaiDiaDiemTheoMaDiaDiem(int maDiaDiem)
        {
            DiaDiem dd =  DiaDiemDAO.LayDiaDiem(maDiaDiem);
            return dd.LoaiDiaDiem.MaLoaiDiaDiem;
        }

        [AjaxPro.AjaxMethod]
        public string layTenLoaiDiaDiemTheoMaDiaDiem(int maDiaDiem)
        {
            DiaDiem dd = DiaDiemDAO.LayDiaDiem(maDiaDiem);
            return dd.LoaiDiaDiem.TenLoaiDiaDiem;
        }



		public void onLoad()
		{


			string sTenDangNhap = (string)Session["TenDangNhap"];

			if (sTenDangNhap == null)
			{
				this.Response.Redirect("~/ThongBao.aspx?noidung=Bạn phải đăng nhập để sử dụng My Map");
				return;
			}

			if (!IsPostBack)
			{
				List<DiaDiem> dsDiaDiem = DiaDiemDAO.LayDanhSachDiaDiem(sTenDangNhap);
				dsDiaDiem = dsDiaDiem.OrderBy(p => p.LoaiDiaDiem.MaLoaiDiaDiem).ToList();

				//////////////////////////////////////////////////////////////////////////
				Literal2.Text += string.Format("<div class='treeview-root' id='treeview-root-0'><div class='hinhanh-root'></div> {0}</div>\n", "Danh sách địa điếm của bạn");

				int maLoaiDiaDiem = 0;
				int count = -1;
				int countNode = 0;
				foreach (DiaDiem idiadiem in dsDiaDiem)
				{

					if (idiadiem.LoaiDiaDiem.MaLoaiDiaDiem != maLoaiDiaDiem)
					{
						//  						TreeNode sub = new TreeNode();
						//  						sub.NavigateUrl = "javascript:KiemDiaDiemGanNhat('" + idiadiem.LoaiDiaDiem.MaLoaiDiaDiem + "')";
						//  						sub.ImageUrl = ".\\img\\thumuc.png";
						//  						sub.Text = idiadiem.LoaiDiaDiem.TenLoaiDiaDiem;
						//  						TreeViewMarker.Nodes[0].ChildNodes.Add(sub);
						//  
						maLoaiDiaDiem = idiadiem.LoaiDiaDiem.MaLoaiDiaDiem;
                        string ganNhat = "javascript:kiemDiaDiemGanNhat(" + maLoaiDiaDiem + ")";

                        Literal2.Text += string.Format("<div class='treeview-loaidiadiem' onclick={2}; id='treeview-loaidiadiem-{1}'><div class='hinhanh-loaidiadiem'></div> {0}</div>\n", idiadiem.LoaiDiaDiem.TenLoaiDiaDiem, idiadiem.LoaiDiaDiem.MaLoaiDiaDiem, ganNhat);
						count++;
					}

					string s = "javascript:openInfoWindowByIndex(" + countNode.ToString() + ")";

					countNode++;
					
                    Literal2.Text += string.Format("<div class='treeview-diadiem'  onclick={1}; id='treeview-diadiem-{2}'><div class='hinhanh-diadiem'></div> {0}</div>\n", idiadiem.TenDiaDiem, s, idiadiem.MaDiaDiem);

                    string _kinhDo = idiadiem.KinhDo.ToString().Replace(',', '.');
                    string _viDo = idiadiem.ViDo.ToString().Replace(',', '.');

                    Literal1.Text += " placeMarker2(" + _viDo + "," + _kinhDo + ",'" + idiadiem.TenDiaDiem + "','" + idiadiem.MaDiaDiem.ToString() +"','" + idiadiem.GhiChu + "','" + countNode.ToString() + "');";
                    
                    //Literal1.Text += " placeMarker2(" + idiadiem.ViDo.ToString() + "," + idiadiem.KinhDo.ToString() + ",'" + idiadiem.TenDiaDiem + "','" + idiadiem.MaDiaDiem.ToString() + "');";

				}

				Literal2.Text += string.Format("<div class='treeview-root' id='treeview-root-1'><div class='hinhanh-root'></div> {0}</div>\n", "Danh sách địa điểm nhận được");
				List<ChiaSeDiaDiem> dsChiaSe = ChiaSeDiaDiemDAO.LayDanhSachChiaSeNguoiNhan(sTenDangNhap);
				dsChiaSe = dsChiaSe.OrderBy(ds => ds.DiaDiem.LoaiDiaDiem.MaLoaiDiaDiem).Distinct().ToList();
				maLoaiDiaDiem = 0;
				count = -1;
				foreach (ChiaSeDiaDiem chiaSe in dsChiaSe)
				{

					if (chiaSe.DiaDiem.LoaiDiaDiem.MaLoaiDiaDiem != maLoaiDiaDiem)
					{
						maLoaiDiaDiem = chiaSe.DiaDiem.LoaiDiaDiem.MaLoaiDiaDiem;

						Literal2.Text += string.Format("<div class='treeview-loaidiadiem' id='treeview-loaidiadiemchiase-{1}'><div class='hinhanh-loaidiadiem'></div> {0}</div>\n", chiaSe.DiaDiem.LoaiDiaDiem.TenLoaiDiaDiem, chiaSe.DiaDiem.LoaiDiaDiem.MaLoaiDiaDiem);
						count++;
					}

					string s = "javascript:openInfoWindowByIndex(" + countNode.ToString() + ")";
					
					Literal2.Text += string.Format("<div class='treeview-diadiem'  onclick={1}; id='treeview-diadiem-{2}'><div class='hinhanh-diadiem'></div> {0}</div>\n", chiaSe.DiaDiem.TenDiaDiem, s, chiaSe.DiaDiem.MaDiaDiem);
					countNode++;

                    string _kinhDo = chiaSe.DiaDiem.KinhDo.ToString().Replace(',', '.');
                    string _viDo = chiaSe.DiaDiem.ViDo.ToString().Replace(',', '.');

					Literal1.Text += " placeMarker2ChiaSe(" + _viDo + "," + _kinhDo + ",'" + chiaSe.DiaDiem.TenDiaDiem + "','" + chiaSe.DiaDiem.MaDiaDiem.ToString() + "','" + countNode.ToString() + "','" + chiaSe.NguoiNhan.TenTaiKhoan + "','" + chiaSe.NguoiChiaSe.MaNguoiDung + "');";
                    
					//Literal1.Text += " placeMarker2(" + chiaSe.DiaDiem.ViDo.ToString() + "," + chiaSe.DiaDiem.KinhDo.ToString() + ",'" + chiaSe.DiaDiem.TenDiaDiem + "','" + chiaSe.DiaDiem.MaDiaDiem.ToString() + "');";
				}
				//////////////////////////////////////////////////////////////////////////

				// 
				// 				TreeNode root = new TreeNode();
				// 				root.ImageUrl = ".\\img\\thumuc.png";
				// 				root.Text = "Danh sách địa điểm của bạn";
				// 				TreeViewMarker.Nodes.Add(root);
				// 
				// 				int maLoaiDiaDiem = 0;
				// 				int count = -1;
				// 				int countNode = 0;
				// 				foreach (DiaDiem idiadiem in dsDiaDiem)
				// 				{
				// 
				// 					if (idiadiem.LoaiDiaDiem.MaLoaiDiaDiem != maLoaiDiaDiem)
				// 					{
				// 						TreeNode sub = new TreeNode();
				// 						sub.NavigateUrl = "javascript:KiemDiaDiemGanNhat('" + idiadiem.LoaiDiaDiem.MaLoaiDiaDiem + "')";
				// 						sub.ImageUrl = ".\\img\\thumuc.png";
				// 						sub.Text = idiadiem.LoaiDiaDiem.TenLoaiDiaDiem;
				// 						TreeViewMarker.Nodes[0].ChildNodes.Add(sub);
				// 
				// 						maLoaiDiaDiem = idiadiem.LoaiDiaDiem.MaLoaiDiaDiem;
				// 						count++;
				// 					}
				// 
				// 					TreeNode subdd = new TreeNode();
				// 					subdd.NavigateUrl = "javascript:openInfoWindowByIndex(" + countNode.ToString() + ")";
				// 					countNode++;
				// 					
				// 					subdd.ImageUrl = ".\\img\\diadiem.png";
				// 					subdd.Text = "<div>" + idiadiem.TenDiaDiem + "</div>;
				// 					subdd.Value = idiadiem.MaDiaDiem.ToString();
				// 					TreeViewMarker.Nodes[0].ChildNodes[count].ChildNodes.Add(subdd);
				// 					Literal1.Text += " placeMarker2(" + idiadiem.ViDo.ToString() + "," + idiadiem.KinhDo.ToString() + ",'" + idiadiem.TenDiaDiem + "','" + idiadiem.MaDiaDiem.ToString() + "');";
				// 				}
				// 
				// 				TreeNode rootNhan = new TreeNode();
				// 				rootNhan.ImageUrl = ".\\img\\thumuc.png";
				// 				rootNhan.Text = "Danh sách địa điểm nhận được";
				// 				TreeViewMarker.Nodes.Add(rootNhan);
				// 
				// 				List<ChiaSeDiaDiem> dsChiaSe = ChiaSeDiaDiemDAO.LayDanhSachChiaSeNguoiNhan(sTenDangNhap);
				// 				dsChiaSe = dsChiaSe.OrderBy(ds => ds.DiaDiem.LoaiDiaDiem.MaLoaiDiaDiem).Distinct().ToList();
				// 
				// 				maLoaiDiaDiem = 0;
				// 				count = -1;
				// 				foreach (ChiaSeDiaDiem chiaSe in dsChiaSe)
				// 				{
				// 
				// 					if (chiaSe.DiaDiem.LoaiDiaDiem.MaLoaiDiaDiem != maLoaiDiaDiem)
				// 					{
				// 						TreeNode sub = new TreeNode();
				// 						sub.ImageUrl = ".\\img\\thumuc.png";
				// 						sub.Text = chiaSe.DiaDiem.LoaiDiaDiem.TenLoaiDiaDiem;
				// 						TreeViewMarker.Nodes[1].ChildNodes.Add(sub);
				// 
				// 						maLoaiDiaDiem = chiaSe.DiaDiem.LoaiDiaDiem.MaLoaiDiaDiem;
				// 						count++;
				// 					}
				// 
				// 					TreeNode subdd = new TreeNode();
				// 					subdd.NavigateUrl = "javascript:openInfoWindowByIndex(" + countNode.ToString() + ")";
				// 					countNode++;
				// 					subdd.ImageUrl = ".\\img\\diadiem.png";
				// 					subdd.Text = chiaSe.DiaDiem.TenDiaDiem;
				// 					subdd.Value = chiaSe.DiaDiem.MaDiaDiem.ToString();
				// 					TreeViewMarker.Nodes[1].ChildNodes[count].ChildNodes.Add(subdd);
				// 					Literal1.Text += " placeMarker2(" + chiaSe.DiaDiem.ViDo.ToString() + "," + chiaSe.DiaDiem.KinhDo.ToString() + ",'" + chiaSe.DiaDiem.TenDiaDiem + "','" + chiaSe.DiaDiem.MaDiaDiem.ToString() + "');";
			}
		}

        [AjaxPro.AjaxMethod]
        public bool themDD(double kinhDo, double viDo, int maLoaidd, String tenDiaDiem, String ghiChu)
        {
            NguoiDung nd = (NguoiDung)Session["NguoiDung"];
            return DiaDiemDAO.ThemDiaDiem(kinhDo, viDo, nd.MaNguoiDung, maLoaidd, tenDiaDiem, ghiChu);
        }

        [AjaxPro.AjaxMethod]
        public bool checkDangNhap()
        {
            return Session["NguoiDung"] != null;
        }

		[AjaxPro.AjaxMethod]
		public bool chapNhanDiaDiem(int maDiaDiem, int tenDangNhap)
		{
			DiaDiem dd = DiaDiemDAO.LayDiaDiem(maDiaDiem);
			return DiaDiemDAO.ThemDiaDiem(dd.KinhDo, dd.ViDo, tenDangNhap, dd.LoaiDiaDiem.MaLoaiDiaDiem, dd.TenDiaDiem, dd.GhiChu);

		}

        [AjaxPro.AjaxMethod]
        public Object[] getDSLoaiDD()
        {
            List<LoaiDiaDiem> list = LoaiDiaDiemDAO.LayDaySachLoaiDiaDiem();
            Object[] DD = new Object[list.Count * 2 + 1];
            DD[0] = list.Count;
            for (int i = 1; i <= list.Count; i++)
            {
                DD[i] = list[i - 1].MaLoaiDiaDiem;
                DD[list.Count + i] = list[i - 1].TenLoaiDiaDiem;
            }

            return DD;
        }

        [AjaxPro.AjaxMethod]
        public bool suaDD(int maDD, double kinhDo, double viDo, int maLoaidd, String tenDiaDiem, String ghiChu)
        {
            NguoiDung nd = (NguoiDung)Session["NguoiDung"];
            return DiaDiemDAO.CapNhat(maDD, kinhDo, viDo, nd.MaNguoiDung, maLoaidd, tenDiaDiem, ghiChu);
        }

        public double toRad(double deg)
        {
            return deg * Math.PI / 180;
        }

        public double tinhKC(double kinhDo, double viDo, double myKinhDo, double myViDo)
        {
            double R = 6371;
            double dLat = toRad(viDo - myViDo);
            double dLon = toRad(kinhDo - myKinhDo);
            double dLat1 = toRad(myViDo);
            double dLat2 = toRad(myKinhDo);
            double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
                    Math.Cos(dLat1) * Math.Cos(dLat1) *
                    Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
            double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            double d = R * c;
            return d;
        }

        [AjaxPro.AjaxMethod]
        public Object[] kiemDDGanNhat(double myKingDo, double myViDo, int maLoaiDD)
        {
            NguoiDung nd = (NguoiDung)Session["NguoiDung"];
            List<DiaDiem> dsDiaDiem = DiaDiemDAO.LayDanhSachDiaDiem(nd.MaNguoiDung, maLoaiDD);
            DiaDiem ganNhat = new DiaDiem();
            ganNhat = dsDiaDiem[0];
            for (int i = 0; i < dsDiaDiem.Count; i++)
            {
                if (tinhKC(dsDiaDiem[i].KinhDo, dsDiaDiem[i].ViDo, myKingDo, myViDo) < tinhKC(ganNhat.KinhDo, ganNhat.ViDo, myKingDo, myViDo))
                {
                    ganNhat = dsDiaDiem[i];
                }
            }
            Object[] DD = new Object[4];
            DD[0] = ganNhat.MaDiaDiem;
            DD[1] = ganNhat.TenDiaDiem;
            DD[2] = ganNhat.KinhDo;
            DD[3] = ganNhat.ViDo;
            return DD;
        }
	}
}
